

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "spi.h"
#include "lcd.h"
#include "spi_sdcard.h"
#include "exfuns.h"
#include "fonts.h"
#include "text.h"
#include "i2s.h"
#include "Lite3D.h"

//i2c_obj_t i2c0_master;

/**
 * @brief       程序入口
 * @param       无
 * @retval      无
 */
void app_main(void)
{
    esp_err_t ret;
    uint8_t key = 0;

    ret = nvs_flash_init();                             /* 初始化NVS */

    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }

    printf("Program Start!!\r\n");
    spi2_init();                                        /* 初始化SPI */
    printf("SPI Init Success!!\r\n");
    lcd_init();                                         /* 初始化LCD */
    printf("LCD Init Success!!\r\n");
    i2s_init();                                         /* I2S初始化 */
    printf("I2S Init Success!!\r\n");
    lcd_clear(BLACK);
    lcd_show_string(30, 110, 200, 16, 16, "SD Card Init!", RED);

    while (sd_spi_init())                               /* 检测不到SD卡 */
    {
        lcd_show_string(30, 110, 200, 16, 16, "SD Card Error!", RED);
        vTaskDelay(500);
        lcd_show_string(30, 130, 200, 16, 16, "Please Check! ", RED);
        vTaskDelay(500);
    }
    
    while (fonts_init())                                /* 检查字库 */
    {
        lcd_clear(WHITE);                               /* 清屏 */
        lcd_show_string(30, 30, 200, 16, 16, "ESP32-S3", RED);
        
        key = fonts_update_font(30, 50, 16, (uint8_t *)"0:", RED);  /* 更新字库 */

        while (key)                                     /* 更新失败 */
        {
            lcd_show_string(30, 50, 200, 16, 16, "Font Update Failed!", RED);
            vTaskDelay(200);
            lcd_fill(20, 50, 200 + 20, 90 + 16, WHITE);
            vTaskDelay(200);
        }

        lcd_show_string(30, 50, 200, 16, 16, "Font Update Success!   ", RED);
        vTaskDelay(1500);
        lcd_clear(WHITE);                               /* 清屏 */
    }

    ret = exfuns_init();                                /* 为fatfs相关变量申请内存 */
    vTaskDelay(500);                                    /* 实验信息显示延时 */
    text_show_string(30, 50, 200, 16, "青青子衿",24,0, RED);
    text_show_string(30, 80, 200, 16, "感谢您来到GameCode", 16, 0, RED);
    text_show_string(30, 100, 200, 16, "锟酱不是酱", 16, 0, RED);
    
    vTaskDelay(1000);

    Lite3D_Test();
    while (1)
    {
        vTaskDelay(1000);
    }
}
